# -*-coding=utf-8-*-
# cps测试 悬浮窗
# python 3.7.4
import time
import tkinter as tk
from pynput import mouse, keyboard


class Ui:
    '''主界面'''

    def __init__(self):
        self.ui = tk.Tk()
        self.ui["bg"] = "light grey"
        self.ui.geometry("240x130+100+100")   # 设置窗口大小，位置
        self.ui.wm_attributes("-topmost", 1)  # 顶层窗口
        self.ui.wm_attributes("-alpha", 0.8)  # 设置透明度
        self.ui.overrideredirect(True)        # 禁止更改界面位置 取消标题栏
        # 以上是关于窗口的设置
        self.mouseListener = MouseListener(self)
        self.mouseListener.listener.start()
        self.keyboardListen()
        # 监听器
        self.changePosAllowed = False  # 是否允许更改界面位置

    def keyboardListen(self):  # 监听键盘 界面快捷键
        hotkey = keyboard.GlobalHotKeys(
            {
                "<alt>+<F1>":self.changeUiPos
            }
        )
        hotkey.start()

    def changeUiPos(self):
        if self.changePosAllowed:
            self.ui.overrideredirect(True)
            self.changePosAllowed = False
        else:
            self.ui.overrideredirect(False)
            self.changePosAllowed = True

    def show(self):  # 窗口显示
        self.CpsLabel_left = TestCpsLabel(self.ui, "left", 10, 15, 100, 100)
        self.CpsLabel_right = TestCpsLabel(self.ui, "right", 130, 15, 100, 100)
        # 放置测试cps的label
        self.ui.mainloop()


class MouseListener:
    '''监听鼠标 显示cps'''
    def __init__(self, ui):
        self.ui = ui   # ui对象 通过此对象改变界面中的标签颜色
        self.listener = mouse.Listener(on_click=self.on_click)  # 监听器
        print("listener prepared!")

    def on_click(self, x, y, button, pressed):
        if str(button) == "Button.left":
            cpsLabel = TestCpsLabel.cpsLabel["left"]
        elif str(button) == "Button.right":
            cpsLabel = TestCpsLabel.cpsLabel["right"]
        else:
            cpsLabel = None
        # 以上获取按键对应的标签
        if pressed:  # 按下 刷新cps并更新颜色
            cpsLabel.freshCps()
            cpsLabel.changeColor("black")
        else:  # 弹起 改回颜色
            cpsLabel.changeColor("grey")
        # 以上对对应标签进行操作


class TestCpsLabel:
    '''显示实时cps的标签'''
    cpsLabel = dict()

    def __init__(self, parent, side, x, y, width, height):
        TestCpsLabel.cpsLabel[side] = self  # 记录 在监听器中调用

        self.testCpsLabel = tk.Label(parent, bg="grey", text="0 CPS")
        self.testCpsLabel.place(x=x, y=y, width=width, height=height)

        self.lastTime = 0  # 上次点击的时间 用于计算点击间隔
        self.nowTime = 0  # 这次点击的时间

    def freshCps(self):  # 点击后刷新点击速度
        self.lastTime, self.nowTime = self.nowTime, time.time()  # 更新时间

        if self.lastTime != 0:
            useTime = self.nowTime - self.lastTime
            self.testCpsLabel["text"] = "%.1f CPS" % (1/useTime)  # 更改点击速度

    def changeColor(self, color):  # 点击后改变颜色 后续可换为渐变色
        self.testCpsLabel["bg"] = color


def main():
    ui = Ui()
    ui.show()


if __name__ == "__main__":
    main()
